package com.lzm.controller;

import com.lzm.domain.Seckill;
import com.lzm.dto.Exposer;
import com.lzm.dto.ResultDto;
import com.lzm.dto.SeckillExecution;
import com.lzm.enums.SeckillStatEnum;
import com.lzm.exception.SeckillException;
import com.lzm.repository.SeckillComplexRepository;
import com.lzm.repository.SeckillRepository;
import com.lzm.service.SeckillService;
import com.lzm.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping(SeckillController.VIEW_PATH)
public class SeckillController {

    public static final String VIEW_PATH = "/seckill";

    @Resource
    private SeckillService seckillService;

    @Resource
    private SeckillRepository seckillRepository;

    @Resource
    private SeckillComplexRepository seckillComplexRepository;

    private final static Logger log = LoggerFactory.getLogger(SeckillController.class);

    @GetMapping({"", "/", "/index"})
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("/index");
        mv.getModelMap().addAttribute("title", "首页");
        return mv;
    }

    @GetMapping("/list")
    public ModelAndView listHead(){
        ModelAndView mv = new ModelAndView("/listHead");
        mv.getModelMap().addAttribute("title", "秒杀列表");
        return mv;
    }

    @PostMapping("/listBody")
    public Page<Seckill> getList(Pageable pageable) {
        Page<Seckill> page = seckillComplexRepository.findAll(pageable);
        log.info("" + page.getContent().size());
        return page;
    }

    /**
     * 返回秒杀列表
     */
    @GetMapping("/{seckillId}/detail")
    public ModelAndView detail(@PathVariable("seckillId") Long seckillId) {
        if (seckillId == null) {
            return new ModelAndView("redirect:/seckill/list");
        }
        Seckill seckill = seckillService.findById(seckillId);
        if (seckill == null) {
            return new ModelAndView("forward:/seckill/list");
        }
        ModelAndView mv = new ModelAndView("/detail");
        mv.getModelMap().addAttribute("seckill",seckill);
        return mv;
    }

    /**
     * 暴露秒杀接口
     */
    @GetMapping(value = "/{seckillId}/exposer", produces = {"application/json;charset=UTF-8"})
    public ResultDto exposer(@PathVariable("seckillId") Long seckillId) {
        Exposer exposer = seckillService.exportSeckillUrl(seckillId);
        return ResultUtil.success(exposer);
    }

    @PostMapping(value = "/{seckillId}/{md5}/execution", produces = {"application/json;charset=UTF-8"})
    public ResultDto execute(@PathVariable("seckillId") Long seckillId,
                             @PathVariable("md5") String md5,
                             @CookieValue(value = "userPhone",required = false) Long userPhone) {
        if (userPhone == null) {
            return ResultUtil.error(SeckillStatEnum.NOT_REGISTER.getDescribe());
        }
        try {
            SeckillExecution execution = seckillService.executeSeckill(seckillId, userPhone, md5);
            return ResultUtil.success(execution);
        }catch (SeckillException e1) {
            return ResultUtil.error(e1.getMessage());
        }catch (Exception e) {
            return ResultUtil.error(SeckillStatEnum.INNER_ERROR.getDescribe());
        }
    }

    /**
     * 获取系统时间
     */
    @GetMapping("/time/now")
    public ResultDto<Long> time() {
        Date now = new Date();
        return ResultUtil.success(now.getTime());
    }
}























